package com.chanel.iamframework.core.test.utils;

import static org.fest.assertions.api.Assertions.assertThat;
import static org.fest.assertions.api.Assertions.failBecauseExceptionWasNotThrown;

import java.util.HashSet;
import java.util.Set;

import javax.validation.ConstraintViolation;

import org.hibernate.validator.method.MethodConstraintViolationException;
import org.springframework.dao.DataIntegrityViolationException;

public class AssertionsUtils {

	public static Set<ConstraintViolation<?>> failBecauseMethodConstraintViolationExceptionWasThrown(Closure closure) {
		Set<ConstraintViolation<?>> constraintViolation = new HashSet<ConstraintViolation<?>>();
		
		try {
			closure.execute();
			failBecauseExceptionWasNotThrown(MethodConstraintViolationException.class);
		} catch (MethodConstraintViolationException e) {
			assertThat(e).isInstanceOf(MethodConstraintViolationException.class);
			constraintViolation.addAll(e.getConstraintViolations());
		}
		
		return constraintViolation;
	}
	
	public static  void failBecauseDataIntegrityViolationExceptionWasThrown(Closure closure) {
		try {
			closure.execute();
			failBecauseExceptionWasNotThrown(DataIntegrityViolationException.class);
		} catch (DataIntegrityViolationException e) {
			assertThat(e).isInstanceOf(DataIntegrityViolationException.class);
		}
	}
	
	public static interface Closure {

		public void execute();
		
	}
	
}
